package br.com.lh.urabuilder.business
{
	import br.com.lh.urabuilder.util.pointchooser.TwoComponentPointChooser;
	import br.com.lh.urabuilder.view.ActionComponent;
	import br.com.lh.urabuilder.view.arrow.ArrowBinderDecorator;
	import br.com.lh.urabuilder.view.arrow.ArrowContainer;
	import br.com.lh.urabuilder.view.arrow.ArrowContainerWithText;
	
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;

	public class ArrowManagerImpl implements ArrowManager
	{
		public var arrowCollection:ArrayCollection=new ArrayCollection();

		private var arrowContainerDictionary:Dictionary=new Dictionary();

		private var parent:Canvas;

		public function ArrowManagerImpl(parent:Canvas)
		{
			this.parent=parent;
		}

		public function insertArrow(startPoint:Point, endPoint:Point, arrowText:String=""):ArrowContainer
		{
			var newArrow:ArrowContainer;
			if (arrowText != "")
			{
				newArrow=new ArrowContainerWithText(startPoint, endPoint, arrowText);
			}
			else
			{
				newArrow=new ArrowContainer(startPoint, endPoint);
			}

			parent.addChild(newArrow);
			arrowCollection.addItem(newArrow);
			return newArrow;
		}

		public function bindStartComponent(arrowContainer:ArrowContainer, startComponent:ActionComponent):void
		{
			var elementIndex:int=arrowCollection.getItemIndex(arrowContainer);
			if (elementIndex == -1)
			{
				return ;
			}
			var arrowBindDecorator:ArrowBinderDecorator=retrieveArrowBinder(arrowContainer);
			arrowBindDecorator.bindStartComponent(startComponent);
			arrowContainer.startComponent=startComponent;
		}

		public function insertBoudedArrow(startComponent:ActionComponent, endComponent:ActionComponent, arrowText:String=""):ArrowContainer
		{
			var pointChooser:TwoComponentPointChooser=new TwoComponentPointChooser(startComponent,endComponent);
			var pointArray:Array=pointChooser.pointArray;
			var arrowContainer:ArrowContainer = insertArrow(pointArray[0],pointArray[1],arrowText);
			bindStartComponent(arrowContainer,startComponent);
			bindEndComponent(arrowContainer,endComponent);
			return arrowContainer;
		}


		public function bindEndComponent(arrowContainer:ArrowContainer, endComponent:ActionComponent):void
		{
			var elementIndex:int=arrowCollection.getItemIndex(arrowContainer);
			if (elementIndex == -1)
			{
				return ;
			}
			var arrowBindDecorator:ArrowBinderDecorator=retrieveArrowBinder(arrowContainer);
			arrowBindDecorator.bindEndComponent(endComponent);
			arrowContainer.endComponent=endComponent;
		}

		private function retrieveArrowBinder(arrowContainer:ArrowContainer):ArrowBinderDecorator
		{
			var decorator:ArrowBinderDecorator=arrowContainerDictionary[arrowContainer];
			if (decorator != null)
			{
				return decorator;
			}
			else
			{
				var arrowDecorator:ArrowBinderDecorator=new ArrowBinderDecorator(arrowContainer);
				arrowContainerDictionary[arrowContainer]=arrowDecorator;
				return arrowDecorator;
			}
		}

		private function getArrowContainer(startPoint:Point, endPoint:Point, arrowText:String):ArrowContainer
		{
			return arrowText == "" ? new ArrowContainer(startPoint, endPoint) : new ArrowContainerWithText(startPoint, endPoint, arrowText);
		}

		public function clearUra():void
		{
			arrowCollection.removeAll();
			arrowContainerDictionary=new Dictionary();
		}

		public function get arrowCount():int
		{
			return arrowCollection.length;
		}

		public function removeArrow(arrowContainer:ArrowContainer):void
		{
			var index:int=arrowCollection.getItemIndex(arrowContainer);
			if (index != -1)
			{
				arrowCollection.removeItemAt(index);
				var arrowBinderDecorator:ArrowBinderDecorator=arrowContainerDictionary[arrowContainer];
				if (arrowBinderDecorator != null)
				{
					arrowBinderDecorator.removeAllBindings();
				}
				arrowContainerDictionary[arrowCollection]=null;
			}
			if (parent.contains(arrowContainer))
			{
				parent.removeChild(arrowContainer);
			}
			arrowContainer.remove();
		}
		
	
		
	}
}


